package com.daojia.daojiasync.dao;


import com.daojia.daojiasync.entity.WorkOrder;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Repository
public interface WorkOrderRepository extends JpaRepository<WorkOrder, Long> {

    @Query(value = "SELECT * FROM work_orders WHERE worderNo = :worderNo", nativeQuery = true)
    Optional<WorkOrder> findByWorderNo(@Param("worderNo") String worderNo);


    @Query(value = "SELECT * FROM work_orders WHERE createTime BETWEEN :startTime AND :endTime ORDER BY createTime", nativeQuery = true)
    List<WorkOrder> findByCreateTimeBetween(@Param("startTime") LocalDateTime startTime,
                                            @Param("endTime") LocalDateTime endTime);

    @Query("SELECT MAX(w.createTime) FROM WorkOrder w")
    LocalDateTime findMaxCreateTime();
    
    @Modifying
    @Query("UPDATE WorkOrder w SET w.sysMark = :sysMark WHERE w.worderNo = :worderNo")
    void updateSysMarkByWorderNo(@Param("worderNo") String worderNo, @Param("sysMark") String sysMark);


}